﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using System.Reflection;
using Newtonsoft.Json;
using IPR_StudyMeetingMrgSys.Models;
using System.Data;
namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class ExamCheckController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        /// <summary>
        /// 用户验证的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/ExamCheck/Add")]
        [HttpPost]
        public IHttpActionResult Add(Model.ExamCheck model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {

                if (new BLL.ExamCheck().Add(model) > 0)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "用户验证增加成功！";
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "用户验证增加失败！";
                }

            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "用户验证增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 用户验证查询
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="startindex"></param>
        /// <param name="endindex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamCheck/Search")]
        public IHttpActionResult Search(string swhere, int startindex, int endindex)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                bool blExist = false;
                DataSet dsSearch = new BLL.ExamCheck().GetListByPage(swhere, "ECID", startindex, endindex);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
                    resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
                    resultMsg.Data = "用户验证不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 用户验证编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ExamCheck/Edit")]
        public IHttpActionResult Edit(Model.ExamCheck model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.ExamCheck().Update(model))
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "用户验证编辑成功！";
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "用户验证编辑失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "用户验证编辑时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/ExamCheck/Del")]
        public IHttpActionResult Del(Model.ExamCheck model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.ExamCheck().Delete(model.ECID))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "用户验证删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "用户验证删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "用户验证删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

    }
}
